<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    {include file='public/_meta' /}
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">{$navs[0]}</a>
                <a href="">{$navs[1]}</a>
                {if isset($navto[2])}
                  <a><cite>{$navto[2]}</cite></a>
                {/if}
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5" method="post" action="{:url('cms/newsclass/index')}">
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" name="keywords"  placeholder="请输入栏目名检索" style="width: 190px;" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-header">
                    <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                    <button class="layui-btn" onclick="xadmin.open('添加权限','./add.html',350,260)"><i class="layui-icon"></i>添加</button>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th width="20">
                                <input type="checkbox" name="" class="selectid"  lay-skin="primary" lay-filter="checkall">
                            </th>
                            <th width="70">ID</th>
                            <th>栏目名</th>
                            <th width="50">排序</th>
                            <th width="80">状态</th>
                            <th width="80">操作</th>
                        </thead>
                        <tbody class="x-cate">
                        {if $list}
                        {foreach $list as $k=>$v}
                        <tr cate-id='{$v.id}' fid='{$v.pid}' >
                            <td>
                                <input type="checkbox" name="" lay-skin="primary">
                            </td>
                            <td>{$v.id}</td>
                            <td>
                                <i class="layui-icon x-show" status='true'>&#xe623;</i>
                                {$v.title}
                            </td>
                            <td><input type="text" class="layui-input x-sort" name="order" onchange="changesort(this,'{$v.id}')"  value="{$v.sort}"></td>
                            <td class="td-status"  width="70">
                                {if $v.status=='关闭'}
                                <span class="layui-btn layui-btn-normal layui-btn-mini layui-btn-disabled">关闭</span>
                                {else}
                                <span class="layui-btn layui-btn-normal layui-btn-mini">开启</span>
                                {/if}
                            </td>
                            <td class="td-manage">
                                {if $v.status=='开启'}
                                <a class="layui-btn layui-btn layui-btn-warm"  onclick="member_stop(this,'{$v.id}')" href="javascript:;"  title="开启">
                                    <i class="layui-icon">&#xe601;</i><span>开启</span>
                                </a>
                                {else}
                                <a class="layui-btn layui-btn layui-btn-warm"  onclick="member_stop(this,'{$v.id}')" href="javascript:;"  title="关闭">
                                    <i class="layui-icon">&#xe62f;</i><span>关闭</span>
                                </a>
                                {/if}
                                <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑','edit.html?id={$v.id}',350,260)" ><i class="layui-icon">&#xe642;</i>编辑</button>
                                <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="member_del(this,'{$v.id}')" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                            </td>
                        </tr>

                        {/foreach}
                        {/if}
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page">
                        <div>
                            {$list->render()}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form'], function(){
        form = layui.form;
        // 监听全选
        form.on('checkbox(checkall)', function(data){

            if(data.elem.checked){
                $('tbody input').prop('checked',true);
            }else{
                $('tbody input').prop('checked',false);
            }
            form.render('checkbox');
        });
    });

    /*停用*/
    function member_stop(obj,id){
        layer.confirm('确认要执行此操作吗？',function(index){

            if($(obj).attr('title')=='开启'){
                $.getJSON("{:url('cms/newsclass/set')}",{'status':0,'id':id},function(res) {
                    if(parseInt(res.statusCode)==200){
                        //发异步把用户状态进行更改
                        $(obj).attr('title','关闭')
                        $(obj).find('i').html('&#xe62f;');

                        $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('关闭');
                        $(obj).parents("tr").find(".td-manage").find('span').html('关闭');
                        $(obj).parents("tr").find(".td-manage").find('a').removeClass('layui-btn-warm');
                        $(obj).parents("tr").find(".td-manage").find('a').addClass('layui-btn-disabled');
                        layer.msg(res.message,{icon: 5,time:1000});
                    }else{
                        layer.msg(res.message,{icon: 5,time:1000});
                    }
                });
            }else{
                $.getJSON("{:url('cms/newsclass/set')}",{'status':1,'id':id},function(res) {
                    if(parseInt(res.statusCode)==200) {
                        $(obj).attr('title', '开启')
                        $(obj).find('i').html('&#xe601;');

                        $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('开启');
                        $(obj).parents("tr").find(".td-manage").find('a').removeClass('layui-btn-disabled');
                        $(obj).parents("tr").find(".td-manage").find('a').addClass('layui-btn-warm');
                        $(obj).parents("tr").find(".td-manage").find('span').html('开启');
                        layer.msg('开启!', {icon: 1, time: 1000});
                    }else{
                        layer.msg(res.message,{icon: 5,time:1000});
                    }
                });
            }
        });
    }

    /*删除*/
    function member_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            //发异步删除数据
            $.getJSON("{:url('cms/newsclass/delete')}",{'ids':id},function(res){
                if(parseInt(res.statusCode) ==200 ){
                    $(obj).parents("tr").remove();
                    layer.msg(res.message,{icon:1,time:1000});
                }else{
                    layer.msg(res.message);
                }
            })

        });
    }



    function delAll (argument) {
        var ids = [];

        // 获取选中的id
        $('tbody input[name=checkbox]').each(function(index, el) {
            if($(this).prop('checked')){
                ids.push($(this).val())
            }
        });

        layer.confirm('确认要删除吗？',function(index){
            //捉到所有被选中的，发异步进行删除
            $.getJSON("{:url('cms/newsclass/delete')}",{'ids':ids.toString()},function(res){

                if(parseInt(res.statusCode)==200){
                    layer.msg(res.message, {icon: 1});
                    $(".layui-form-checked").not('.header').parents('tr').remove();
                }else{
                    layer.msg(res.message, {icon: 2});
                }
            })
        });
    }

    // 分类展开收起的分类的逻辑
    //
    $(function(){
        $("tbody.x-cate tr[fid!='0']").hide();
        // 栏目多级显示效果
        $('.x-show').click(function () {
            if($(this).attr('status')=='true'){
                $(this).html('&#xe625;');
                $(this).attr('status','false');
                cateId = $(this).parents('tr').attr('cate-id');
                $("tbody tr[fid="+cateId+"]").show();
            }else{
                cateIds = [];
                $(this).html('&#xe623;');
                $(this).attr('status','true');
                cateId = $(this).parents('tr').attr('cate-id');
                getCateId(cateId);
                for (var i in cateIds) {
                    $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                }
            }
        })
    })

    var cateIds = [];
    function getCateId(cateId) {
        $("tbody tr[fid="+cateId+"]").each(function(index, el) {
            id = $(el).attr('cate-id');
            cateIds.push(id);
            getCateId(id);
        });
    }

    /* 排序 */
    function changesort(obj,id){
        $.getJSON("{:url('cms/newsclass/sort')}",{'id':id,'sort':obj.value},function(res){
            if(parseInt(res.statusCode)==200){
                //发异步删除数据
                location.reload();
                layer.msg(res.message,{icon:1});
            }else{
                layer.msg(res.message);
            }
        })
    }

</script>
</body>
</html>
